package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.entity.TbStudent;
import com.qf.entity.TbTeacher;
import com.qf.service.TbStudentService;
import com.qf.mapper.TbStudentMapper;
import com.qf.utils.Result;
import org.apache.ibatis.annotations.Mapper;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

/**
 *
 */
@Service
public class TbStudentServiceImpl extends ServiceImpl<TbStudentMapper, TbStudent>
    implements TbStudentService{

    private final TbStudentMapper tbStudentMapper;

    public TbStudentServiceImpl(TbStudentMapper studentMapper) {
        this.tbStudentMapper = studentMapper;
    }

    @Override
    public Result login(String snum,String password) {
        //1.严格的参数校验
        LambdaQueryWrapper<TbStudent> lqw=new LambdaQueryWrapper<>();
        lqw.eq(TbStudent::getSnum,snum);
        TbStudent tbStudent = tbStudentMapper.selectOne(lqw);



        String spassword = tbStudent.getSpassword();



        return spassword.equals(password)?Result.success("登录成功"):Result.error("登录失败");
    }

    @Override
    public Result updatepassword(TbStudent tbStudent) {
        LambdaQueryWrapper<TbStudent> lqw=new LambdaQueryWrapper<>();
        System.out.println(tbStudent.getEmail());
        lqw.eq(TbStudent::getEmail,tbStudent.getEmail());
        TbStudent student = tbStudentMapper.selectOne(lqw);tbStudentMapper.selectOne(lqw);

        if(student!=null){
            student.setSpassword(student.getSpassword());
            tbStudentMapper.update(student,lqw);
            return Result.success("更新成功");
        }



        return Result.success("更新失败");
    }

    @Override
    public Result getList(Long pageNum,Long pageSize,Long id) {

        Page<TbStudent> page=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<TbStudent> lqw =new LambdaQueryWrapper();
        lqw.eq(TbStudent::getSid,id);
        page= tbStudentMapper.selectPage(page,lqw);
        return Result.success("查询成功",page);
    }

    @Override
    public Result deleteStudentListById(Long id) {
        LambdaQueryWrapper<TbStudent> lqw=new LambdaQueryWrapper<>();
        lqw.eq(TbStudent::getSid,id);
        tbStudentMapper.delete(lqw);
        return Result.success("删除成功");
    }

    @Override
    public Result updateStudentListById(@RequestBody TbStudent tbStudent) {

        LambdaQueryWrapper<TbStudent> lqw =new LambdaQueryWrapper<>();
        lqw.eq(TbStudent::getSid,tbStudent.getSid());
        TbStudent SelectTbStudent = tbStudentMapper.selectOne(lqw);
        if(SelectTbStudent!=null){

            tbStudentMapper.updateById(tbStudent);
        }


        return Result.success("添加成功");
    }

    @Override
    public Result findAllStudents() {
        List<TbStudent> tbStudents = tbStudentMapper.selectList(null);
        return Result.success("查询成功",tbStudents);
    }

    @Override
    public Result downLoad(String studentid, HttpSession session, HttpServletResponse response)  {
        String filename = tbStudentMapper.downLoad(studentid);
        String realPath = session.getServletContext().getRealPath("/file");
        response.setHeader("content-disposition","attachment;filename="+filename);
        try {
            IOUtils.copy(new FileInputStream(realPath+ File.separator+filename),response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.success("下载成功");
    }




}




